package DBLayer;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;



import ModelLayer.Employee;
import ModelLayer.Holiday;

public class DBHoliday implements IFDBHoliday 
{
	private  Connection con;
	
	public DBHoliday()
	{
		con = DBConnection.getInstance().getDBcon();
	}
	
	public Holiday getHoliday(int empId, Date date)
	{
		return singleWhere("ID = DayOffID AND EmployeeID = " + empId + " AND Date = '" + date.toString() + "'");
	}
	
	private Holiday singleWhere(String wclause)
	{
		ResultSet results;
		Holiday hDay = null;
		String query = buildQuery(wclause);
		
		try
		{
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);
			
			if(results.next())
			{
				hDay = buildHoliday(results);
			}
			
			stmt.close();
			
		}
		
		catch(Exception e)
		{
			System.out.println("Query Exception - Select: "+e);
			e.printStackTrace();
		}
		
		return hDay;
		
	}
	private Holiday buildHoliday(ResultSet result)
	{
		Holiday holiday = new Holiday();
		
		try
		{
			holiday.setId(result.getInt("ID"));
			holiday.setType(result.getString("Type"));
			holiday.setEmployee(new Employee(result.getInt("EmployeeID")));
			holiday.setDate(result.getDate("Date"));
			holiday.setStarTime(result.getTime("StartTime"));
			holiday.setEndTime(result.getTime("EndTime"));
			holiday.setAllDay(result.getBoolean("AllDay"));
		   
		    
		}
		catch(Exception e)
		{
			
		}
		
		return holiday;
	}
	
	
	private String buildQuery(String query)
	{
		String wQuery = "SELECT ID, Type, EmployeeId, Date, StartTime, EndTime, AllDay FROM DayOff, Holiday";
		
		if(query.length() > 0)
		{
			wQuery = wQuery + " WHERE " + query;
		}
		
		return wQuery;
	}
	
}
